package br.com.comdoc.repositorio;

//@author Jorge
import br.com.comdoc.DAO.LinkDB;
import br.com.comdoc.entidade.Endereco;
import br.com.comdoc.interfaces.InterfaceEndereco;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;

public class RepositorioEndereco implements InterfaceEndereco {

    private Connection conn;
    private String query;
    private PreparedStatement stmt;
    private ResultSet rs;
    private Endereco endereco;
    private ArrayList list = new ArrayList();

    @Override
    public void salvarEndereco(String rua, String CEP, String bairro, String cidade,
            String estado, String paiz) throws ClassNotFoundException, SQLException {

        endereco = new Endereco(rua, CEP, bairro, cidade, estado, paiz);

        conn = LinkDB.getConnection();

        query = "INSERT INTO endereco(RUAENDERECO, CEPENDERECO, BAIRROENDERECO, "
                + "CIDADEENDERECO, ESTADOENDERECO, PAISENDERECO) VALUES(?, ?, ?, ?, ?, ?)";

        stmt = conn.prepareStatement(query);

        stmt.setString(1, endereco.getRua());
        stmt.setString(2, endereco.getCEP());
        stmt.setString(3, endereco.getBairro());
        stmt.setString(4, endereco.getCidade());
        stmt.setString(5, endereco.getEstado());
        stmt.setString(6, endereco.getPais());

        stmt.executeUpdate();

        conn.close();
        stmt.close();

    }

    @Override
    public void alterarEndereco(int id, String rua, String CEP, String bairro, String cidade,
            String estado, String paiz) throws ClassNotFoundException, SQLException {

        endereco = new Endereco(rua, CEP, bairro, cidade, estado, paiz);
        endereco.setId(id);

        conn = LinkDB.getConnection();

        query = "UPDATE endereco SET RUAENDERECO = ?, CEPENDERECO = ?, BAIRROENDERECO = ?, "
                + "CIDADEENDERECO = ?, ESTADOENDERECO = ?, PAIZENDERECO = ? WHERE IDENDERECO = ?";

        stmt = conn.prepareStatement(query);

        stmt.setString(1, endereco.getRua());
        stmt.setString(2, endereco.getCEP());
        stmt.setString(3, endereco.getBairro());
        stmt.setString(4, endereco.getCidade());
        stmt.setString(5, endereco.getEstado());
        stmt.setString(6, endereco.getPais());
        stmt.setInt(7, endereco.getId());

        stmt.executeUpdate();

        JOptionPane.showMessageDialog(null, "Alterado com sucesso!");

        stmt.close();
        conn.close();

    }

    @Override
    public void deletarEndereco(int id) throws ClassNotFoundException, SQLException {

        endereco = new Endereco();
        endereco.setId(id);

        conn = LinkDB.getConnection();
        query = "UPDATE endereco SET STATUSENDERECO = '0'"
                + "WHERE IDENDERECO = ?";

        stmt = conn.prepareStatement(query);

        stmt.setInt(1, endereco.getId());

        stmt.executeUpdate();

        JOptionPane.showMessageDialog(null, "Deletado com sucesso!");

        conn.close();
        stmt.close();

    }

    @Override
    public ArrayList consultarEnderecoPorId(int id) throws ClassNotFoundException,
            SQLException {

        endereco = new Endereco();

        endereco.setId(id);

        conn = LinkDB.getConnection();

        query = "SELECT * FROM endereco WHERE endereco.IDENDERECO = ?";

        stmt = conn.prepareStatement(query);
        stmt.setInt(1, endereco.getId());

        rs = stmt.executeQuery();

        while (rs.next()) {

            list.add(rs.getString("IDENDERECO"));
            list.add(rs.getString("RUAENDERECO"));
            list.add(rs.getString("CEPENDERECO"));
            list.add(rs.getString("BAIRROENDERECO"));
            list.add(rs.getString("CIDADEENDERECO"));
            list.add(rs.getString("ESTADOENDERECO"));
            list.add(rs.getString("PAIZENDERECO"));
            list.add(rs.getString("STATUSENDERECO"));

        }

        conn.close();
        stmt.close();
        rs.close();

        return list;

    }

    @Override
    public int returnIdEndereco() throws ClassNotFoundException,
            SQLException {

        conn = LinkDB.getConnection();

        query = "SELECT * FROM endereco ORDER BY endereco.IDENDERECO DESC LIMIT 0, 1;";

        stmt = conn.prepareStatement(query);

        rs = stmt.executeQuery();

        if (rs.next()) {
            String var = rs.getString("endereco.IDENDERECO");
            int id = Integer.parseInt(var);
            return id;
        } else {
            return 0;
        }
    }
}
